/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.sales.hanserver.action.hancadsa.finished.query;

import br.com.sales.hanserver.action.hancadsa.finished.consts.FinishedSaleConsts;
import static br.com.sales.hanserver.action.hancadsa.finished.consts.FinishedSaleConsts.INSERT_ITEM_SALE;
import static br.com.sales.hanserver.action.hancadsa.finished.consts.FinishedSaleConsts.INSERT_SALE;
import static br.com.sales.hanserver.action.hancadsa.finished.consts.FinishedSaleConsts.UPDATE_PRODUCT_SALES;
import br.com.sales.hanserver.dao.Dao;
import static br.com.sales.hanserver.dao.Dao.getConn;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author v
 */
public class FinishedSaleQuery extends Dao {

    public FinishedSaleQuery() {
    }

    protected void insertSale(long sequence, Timestamp date, Long seller,
            Long clientPF, Double vlTotal, int desc) {
        try {

            PreparedStatement preparedStmt = getConn().prepareStatement(INSERT_SALE);
            preparedStmt.setLong(1, sequence);
            preparedStmt.setTimestamp(2, date);
            preparedStmt.setLong(3, seller);
            preparedStmt.setLong(4, clientPF);
            preparedStmt.setObject(5, vlTotal);
            preparedStmt.setInt(6, desc);

            preparedStmt.executeUpdate();

        } catch (Exception e) {
            Logger.getLogger(FinishedSaleQuery.class.getName()).log(Level.INFO, "Error insert sales", e);
        } finally {
            closeConn();
        }
    }

    protected void insertItemSale(Double price, Integer count, Long seqSale, Long seqProduct) {
        try {

            PreparedStatement preparedStmt = getConn().prepareStatement(INSERT_ITEM_SALE);
            preparedStmt.setObject(1, price);
            preparedStmt.setInt(2, count);
            preparedStmt.setLong(3, seqSale);
            preparedStmt.setLong(4, seqProduct);

            preparedStmt.executeUpdate();
        } catch (Exception e) {
            Logger.getLogger(FinishedSaleQuery.class.getName()).log(Level.INFO, "Error insert sales", e);
        } finally {
            closeConn();
        }
    }

    public long getMaxSequenceSale() {
        try {
            Statement st = getConn().createStatement();
            ResultSet rs = st.executeQuery(FinishedSaleConsts.SELECT_MAX_SEQUENCE);
            rs.next();
            return rs.getLong("sequence");

        } catch (Exception ex) {
            Logger.getLogger(FinishedSaleQuery.class.getName()).log(Level.INFO, "Error max NR_SEQUENCE", ex);
        } finally {
            closeConn();
        }
        return 0;
    }

    public long getSeller(String nmUser) {
        try {
            PreparedStatement st = getConn().prepareStatement(FinishedSaleConsts.SELLER_BY_USER);
            st.setString(1, nmUser);

            ResultSet rs = st.executeQuery();
            rs.next();
            return rs.getLong("SEQUENCE");

        } catch (SQLException ex) {
            Logger.getLogger(FinishedSaleQuery.class.getName()).log(Level.INFO, null, ex);
        } finally {
            closeConn();
        }
        return 0;
    }

    public void updateProduct(Integer countStock, Long sequence) {
        try {
            PreparedStatement st = getConn().prepareStatement(UPDATE_PRODUCT_SALES);
            st.setObject(1, countStock);
            st.setLong(2, sequence);

            st.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(FinishedSaleQuery.class.getName()).log(Level.INFO, null, ex);
        } finally {
            closeConn();
        }
    }
}
